// pages/user/user.js
import { Base } from '../../utils/base.js';
import { Video } from './video-model.js';

const app = getApp()
const video = new Video()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        
        videoPlay: true, //默认播放视频
        list: [],
        index: 0,
        nowVideo: null,
        page: 1,
        size:10,
        hasMore: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.aldstat.sendEvent('进入购物车');
        let sessionId = wx.getStorageSync("sessionId");
        if (sessionId === "") {
            wx.redirectTo({
                url: '/pages/getUserInfo/getUserInfo'
            })
            return
        }

        wx.showLoading({
            title: '加载中...',
        })

        this.getList();

    },

    onReady: function (res) {
      this.videoContext = wx.createVideoContext('myVideo')
      this.videoContext.requestFullScreen({ direction: 0 })
      
    },
    onShow:function(){
      this.getList();
    },
    getList() {
        let { page, size, list, hasMore, index } = this.data;
        let params = {
            page,
            size
        }
        if (hasMore) {
            video.goodsVideoList(params, res => {
                wx.hideLoading();
                if (res.status) {
                    let data = res.data;
                    let total = res.total;

                    if (page > Math.ceil(total / size)) {
                        console.log("结束了");
                        this.setData({
                            index: 0,
                            hasMore: false
                        })
                    } else {
                        data = [...list, ...data]
                        this.setData({
                            list: data
                        })
                     
                    }
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                }
            })
        } else {
            this.setData({
                index: 0
            })
        }
    },
    initData() {
        let { list, index, page } = this.data;

        // this.setData({
        //   show:false
        // })
        if (index >= list.length) {
            page = page + 1
            this.setData({
                page
            })
            this.getList();
            return;
        }

        if (list.length > 0 && index <= list.length) {
            let nowVideo = list[index]
            console.log("nowVideo", nowVideo)
          
            //记录点击
            this.videoClickIncr("" + nowVideo.relation, "view");
            index++;

            this.setData({
                nowVideo,
                index,
                videoPlay: true,
            })
            
        }




    },
    //查看详情
    onProductDetail() {

        var {
            nowVideo
        } = this.data;
        //记录点击
        this.videoClickIncr("" + nowVideo.goodsId, "detail");
        wx.navigateTo({
            url: '/pages/product/product?id=' + nowVideo.goodsId,
        })
    },
    downLoad() {
        let { nowVideo } = this.data;

        //记录点击
        this.videoClickIncr("" + nowVideo.relation, "save");
        wx.getSetting({
            success: (res) => {
                console.log("res--------", res, Object.keys(res.authSetting).indexOf('scope.writePhotosAlbum'))
                if (Object.keys(res.authSetting).indexOf('scope.writePhotosAlbum') < 0 || res.authSetting["scope.writePhotosAlbum"]) {

                    wx.showToast({
                        title: '视频保存中',
                        icon: 'loading'
                    })
                    // console.log("downloadFile", downloadFile)
                    wx.downloadFile({
                        url: nowVideo.videoUrl,
                        success: function (res) {
                            var path = res.tempFilePath
                            console.log(path)
                            wx.saveVideoToPhotosAlbum({
                                filePath: path,
                                success(res) {
                                    console.log("视频保存成功")
                                    wx.showToast({
                                        title: '视频保存成功'
                                    })
                                },
                                fail(res) {
                                    console.log("视频保存失败", res)
                                    wx.showToast({
                                        title: '视频保存失败' + res,
                                        icon: "none"
                                    })
                                }
                            })
                        },
                        fail(res) {
                            console.log("本地保存失败" + res)
                            wx.showToast({
                                title: JSON.stringify(res),
                                icon: "none"
                            })
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '储存视频权限未开启，点击开启？',
                        success(res) {
                            if (res.confirm) {
                                wx.openSetting({

                                })
                            }
                        }
                    })
                }
            },
            fail: function (res) {
                console.log('图片下载失败', res, val.url)
            }
        })
    },
    nextVideo() {
        this.initData()
    },
    //视频暂停播放
    handVideo() {
        let {
            videoPlay
        } = this.data;
        if (videoPlay) {
            this.videoContext.pause();
        } else {
            this.videoContext.play();
        }

    },
    onReachBottom() {
        wx.showToast({
            title: '触底',
        })
    },
    //视频播放执行
    VideoPlay() {
        let {
            videoPlay
        } = this.data;
        this.setData({
            videoPlay: true
        })
    },
    //视频暂停执行
    VideoPause() {
        let {
            videoPlay
        } = this.data;
        this.setData({
            videoPlay: false
        })
    },
    //记录点击事件
    videoClickIncr(relation, actionType) {
        let params = {
            "relation": relation ? relation : "",
            "actionType": actionType ? actionType : "",
        }
        video.videoClickIncr(params, res => {
            if (res.status) {
                console.log("记录成功");
            }
        })
    },
    goIndex() {
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
  goActivity(){
    let { nowVideo } = this.data;
    wx.navigateTo({
      url: "/pages/home/home?id=" + nowVideo.activityId
    })
  }
})